package com.zhongzhi.javafx.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import java.util.ResourceBundle;

import com.zhongzhi.bean.Configuration;
import com.zhongzhi.common.Common;
import com.zhongzhi.util.ConfigUtil;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import lombok.extern.log4j.Log4j;
@Log4j
public class parameterController implements Initializable{
	//本机站号
	@FXML
	private TextField serial;
	//服务器ip
	@FXML
	private TextField ServerId;
	//服务器端口
	@FXML
	private TextField ServerProt;
	//同步数据库
	@FXML
	private Button synchronization;
	//关闭
	@FXML
	private Button off;
	//确定
	@FXML
	private Button confirm;
	//错误提示服务器ip
	@FXML
	private Label serveridtext;
	//错误提示服务器端口
	@FXML
	private Label serverprottext;




	@FXML
	private TextField scope1;

	@FXML
	private TextField scope2;

	@FXML
	private TextField scope3;

	@FXML
	private TextField scope4;

	@FXML
	private TextField scope5;

	@FXML
	private TextField scope6;

	@FXML
	private TextField mod1;

	@FXML
	private TextField mod2;

	@FXML
	private TextField mod3;

	@FXML
	private TextField mod4;

	@FXML
	private TextField mod5;

	@FXML
	private TextField mod6;

	@FXML
	private TextField mod7;

	
	
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		ConfigUtil configUtil = new ConfigUtil();
		configUtil.getConfiguration();
		Configuration configuration = Common.getConfiguration();
		serial.setText(configuration.getProperty());
		ServerId.setText(configuration.getServerId());
		ServerProt.setText(configuration.getServerProt());
		
		//2021/2/25
		scope1.setText(configuration.getScope1().toString());
		scope2.setText(configuration.getScope2().toString());
		scope3.setText(configuration.getScope3().toString());
		scope4.setText(configuration.getScope4().toString());
		scope5.setText(configuration.getScope5().toString());
		scope6.setText(configuration.getScope6().toString());
		mod1.setText(configuration.getMod1().toString());
		mod2.setText(configuration.getMod2().toString());
		mod3.setText(configuration.getMod3().toString());
		mod4.setText(configuration.getMod4().toString());
		mod5.setText(configuration.getMod5().toString());
		mod6.setText(configuration.getMod6().toString());
		mod7.setText(configuration.getMod7().toString());

		confirm.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {

				try {
					String text = ServerId.getText();
					String text2 = ServerProt.getText();
					boolean a =false;
					boolean b =false;

					if (text.trim().matches("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}")==false) {
						serveridtext.setText("服务器ip地址格式不正确");
					}else {
						serveridtext.setText("");
						a=true;
					}
					if (text2.trim().matches("-?[1-9]\\d*")==false) {
						serverprottext.setText("服务器端口必须为纯数字");
					}else {
						serverprottext.setText("");
						b=true;
					}

					if (a&&b) {
						ConfigUtil configUtil = new ConfigUtil();
//						configUtil.setConfiguration(serial.getText(), ServerId.getText(), ServerProt.getText());
						configUtil.setConfiguration(
								serial.getText(), 
								ServerId.getText(), 
								ServerProt.getText(),
								scope1.getText(),
								scope2.getText(),
								scope3.getText(),
								scope4.getText(),
								scope5.getText(),
								scope6.getText(),
								mod1.getText(),
								mod2.getText(),
								mod3.getText(),
								mod4.getText(),
								mod5.getText(),
								mod6.getText(),
								mod7.getText());
						
						

						Stage window = (Stage) off.getScene().getWindow();
						window.close();
					}
				} catch (Exception e) {
					log.error("参数设置画面,修改参数失败:"+e);
				}
			}
		});
		off.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				Stage window = (Stage) off.getScene().getWindow();
				window.close();
			}
		});





	}

}
